//
//  SEExamineAlertView.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/6/2.
//

import UIKit

enum SEExamineAlertViewType {
    /// 0:生成中 1:报告已存在 2:没有报告 3:尚有检查单未完成,无法生成报告
    case creating
    case finish
    case waticreate
    case unsupport
    case createFailed(_ msg: String)
    case unknown

    var title: String {
        switch self {
        case .creating:
            return "检查报告生成中"
        case .finish:
            return "检查报告已生成"
        case .waticreate:
            return "检查报告未生成"
        case .unsupport:
            return "无法生成报告"
        case .createFailed:
            return "生成检查报告失败"
        default:
            return ""
        }
    }

    var memo: String {
        switch self {
        case .creating:
            return "您的检查报告正在生成\n请等待5分钟后再点击查看"
        case .finish:
            return "您的检查报告已生成\n点击查看报告"
        case .waticreate:
            return "您的检查报告还未生成\n是否立即生成报告(限时免费)"
        case .unsupport:
            return "尚有检查单未完成\n无法生成报告"
        case .createFailed(let msg):
            return msg
        default:
            return ""
        }
    }

    var hiddenCancel: Bool {
        switch self {
        case .waticreate, .finish, .createFailed: return false
        default: return true
        }
    }

    var confirmBtnTitle: String {
        switch self {
        case .waticreate: return "生成报告"
        case .finish: return "查看报告"
        case .createFailed: return "重试"
        default: return "确定"
        }
    }

    var logoImgName: String {
        switch self {
        case .createFailed: return "examine_aireport_failed"
        default: return "examine_aireport_wait"
        }
    }
}

extension SEExamineAlertViewType: RawRepresentable {
    typealias RawValue = Int

    init?(rawValue: RawValue) {
        switch rawValue {
        case 0: self = .creating
        case 1: self = .finish
        case 2: self = .waticreate
        case 3: self = .unsupport
        case 4: self = .createFailed("")
        default: return nil
        }
    }

    var rawValue: RawValue {
        switch self {
        case .creating: return 0
        case .finish: return 1
        case .waticreate: return 2
        case .unsupport: return 3
        case .createFailed: return 4
        default: return 5
        }
    }
}

class SEExamineAlertView: UIView {

    override func awakeFromNib() {
        super.awakeFromNib()
        alpha = 0.0
    }

    @IBOutlet weak var contentView: UIView! {
        didSet {
            contentView.frame.origin.y = -300
            contentView.layer.cornerRadius = 14
            contentView.layer.masksToBounds = true
        }
    }

    @IBAction func confirmBtnAction(_ sender: UIButton) {
        confirmBtnHandler?(type)
        dismiss()
    }

    @IBAction func cancelBtnAction(_ sender: UIButton) {
        cancelBtnHandler?(type)
        dismiss()
    }

    @IBOutlet weak var confirmBtn: UIButton!

    @IBOutlet weak var cancelBtn: UIButton!
    
    @IBOutlet weak var imageView: UIImageView!

    @IBOutlet weak var titleLabel: UILabel!

    @IBOutlet weak var memoLabel: UILabel!

    var confirmBtnHandler: ((SEExamineAlertViewType) -> Void)?
    var cancelBtnHandler: ((SEExamineAlertViewType) -> Void)?

    private var type: SEExamineAlertViewType = .unknown {
        didSet {
            imageView.image = UIImage(named: type.logoImgName)
            titleLabel.text = type.title
            memoLabel.text = type.memo
            cancelBtn.isHidden = type.hiddenCancel
            confirmBtn.setTitle(type.confirmBtnTitle, for: .normal)
        }
    }

    func dismiss() {
        UIView.animate(withDuration: 0.25, delay: 0, options: .curveLinear, animations: {
            self.contentView.transform = CGAffineTransform(scaleX: 0.0001, y: 0.0001)
            self.alpha = 0.0
        }) { _ in
            self.removeFromSuperview()
        }
    }


    func show(_ type: SEExamineAlertViewType) {
        self.type = type
        mainWindow?.addSubview(self)
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: .curveEaseOut) {
            self.alpha = 1.0
            self.contentView.center = self.center
        } completion: { finish in

        }
    }
}
